//
//  ChatModel.m
//  WChat
//
//  Created by lw on 2018/7/31.
//  Copyright © 2018年 mjsfax. All rights reserved.
//

#import "ChatModel.h"

@implementation ChatModel

- (void)setMessage:(EMMessage *)message {
    _message = message;
    NSString *userName = [[EaseMob sharedInstance].chatManager loginInfo][@"username"];
    if ([userName isEqualToString:message.from]) {
        self.me = YES;
    } else {
        self.me = NO;
    }
    
    id<IEMMessageBody> msgBody = message.messageBodies.firstObject;
    self.chatType = msgBody.messageBodyType;
    switch (msgBody.messageBodyType) {
        case eMessageBodyType_Text:
        {
            // 收到的文字消息
            NSString *txt = ((EMTextMessageBody *)msgBody).text;
//            NSLog(@"收到的文字是 txt -- %@",txt);
            self.contentText = txt;
            CGRect rect = [txt boundingRectWithSize:CGSizeMake(SCREENWIDTH-40, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil];
            self.cellHeight = rect.size.height + 20;
        }
            break;
        case eMessageBodyType_Image:
        {
            // 得到一个图片消息body
            EMImageMessageBody *body = ((EMImageMessageBody *)msgBody);
//            NSLog(@"大图remote路径 -- %@"   ,body.remotePath);
//            NSLog(@"大图local路径 -- %@"    ,body.localPath);  // 需要使用SDK提供的下载方法后才会存在
//            NSLog(@"大图的secret -- %@"    ,body.secretKey);
//            NSLog(@"大图的W -- %f ,大图的H -- %f",body.size.width,body.size.height);
//            NSLog(@"大图的下载状态 -- %lu",body.attachmentDownloadStatus);
//
//            // 缩略图sdk会自动下载
//            NSLog(@"小图remote路径 -- %@"   ,body.thumbnailRemotePath);
//            NSLog(@"小图local路径 -- %@"    ,body.thumbnailLocalPath);
//            NSLog(@"小图的secret -- %@"    ,body.thumbnailSecretKey);
//            NSLog(@"小图的W -- %f ,大图的H -- %f",body.thumbnailSize.width,body.thumbnailSize.height);
//            NSLog(@"小图的下载状态 -- %lu",body.thumbnailDownloadStatus);
            
            if ([[NSFileManager defaultManager] fileExistsAtPath:body.localPath]) {
                self.localImageUrl = body.localPath;
            }
            self.remoteImageUrl = body.remotePath;
            if ([[NSFileManager defaultManager] fileExistsAtPath:body.thumbnailLocalPath]) {
                self.localThumbnailUrl = body.thumbnailLocalPath;
            }
        
            self.remoteThumbnailUrl = body.thumbnailRemotePath;
            self.vertical = body.thumbnailSize.width > body.thumbnailSize.height;
            CGSize imageSize;
            if (self.vertical) {
                self.imageRect = CGRectMake(0, 0, 100, 50);
                self.cellHeight = 55;
                imageSize = CGSizeMake(100, 50);
            } else {
                self.imageRect = CGRectMake(0, 0, 50, 100);
                self.cellHeight = 105;
                imageSize = CGSizeMake(50, 100);
            }
            if (self.isMe) {
                self.imageRect = CGRectMake(SCREENWIDTH-imageSize.width-30, 5, imageSize.width, imageSize.height);
            } else {
                self.imageRect = CGRectMake(30, 5, imageSize.width, imageSize.height);
            }
            
        }
            break;
        case eMessageBodyType_Location:
        {
            EMLocationMessageBody *body = (EMLocationMessageBody *)msgBody;
            NSLog(@"纬度-- %f",body.latitude);
            NSLog(@"经度-- %f",body.longitude);
            NSLog(@"地址-- %@",body.address);
        }
            break;
        case eMessageBodyType_Voice:
        {
            // 音频SDK会自动下载
            EMVoiceMessageBody *body = (EMVoiceMessageBody *)msgBody;
            NSLog(@"音频remote路径 -- %@"      ,body.remotePath);
            NSLog(@"音频local路径 -- %@"       ,body.localPath); // 需要使用SDK提供的下载方法后才会存在（音频会自动调用）
            NSLog(@"音频的secret -- %@"        ,body.secretKey);
            NSLog(@"音频文件大小 -- %lld"       ,body.fileLength);
            NSLog(@"音频文件的下载状态 -- %lu"   ,body.attachmentDownloadStatus);
            NSLog(@"音频的时间长度 -- %lu"      ,body.duration);
            self.duration = body.duration;
            if ([[NSFileManager defaultManager] fileExistsAtPath:body.localPath]) {
                self.voicePath = body.localPath;
            } else {
                self.voicePath = body.remotePath;
            }
            self.cellHeight = 50;
        }
            break;
        case eMessageBodyType_Video:
        {
            EMVideoMessageBody *body = (EMVideoMessageBody *)msgBody;
            
            NSLog(@"视频remote路径 -- %@"      ,body.remotePath);
            NSLog(@"视频local路径 -- %@"       ,body.localPath); // 需要使用SDK提供的下载方法后才会存在
            NSLog(@"视频的secret -- %@"        ,body.secretKey);
            NSLog(@"视频文件大小 -- %lld"       ,body.fileLength);
            NSLog(@"视频文件的下载状态 -- %lu"   ,body.attachmentDownloadStatus);
            NSLog(@"视频的时间长度 -- %lu"      ,body.duration);
            NSLog(@"视频的W -- %f ,视频的H -- %f", body.size.width, body.size.height);
            
            // 缩略图sdk会自动下载
            NSLog(@"缩略图的remote路径 -- %@"     ,body.thumbnailRemotePath);
            NSLog(@"缩略图的local路径 -- %@"      ,body.thumbnailRemotePath);
            NSLog(@"缩略图的secret -- %@"        ,body.thumbnailSecretKey);
            NSLog(@"缩略图的下载状态 -- %lu"      ,body.thumbnailDownloadStatus);
        }
            break;
        case eMessageBodyType_File:
        {
            EMFileMessageBody *body = (EMFileMessageBody *)msgBody;
            NSLog(@"文件remote路径 -- %@"      ,body.remotePath);
            NSLog(@"文件local路径 -- %@"       ,body.localPath); // 需要使用SDK提供的下载方法后才会存在
            NSLog(@"文件的secret -- %@"        ,body.secretKey);
            NSLog(@"文件文件大小 -- %lld"       ,body.fileLength);
            NSLog(@"文件文件的下载状态 -- %lu"   ,body.attachmentDownloadStatus);
        }
            break;
            
        default:
            break;
    }
}

@end
